{%- extends "base.html" -%}

{%- import 'macros.html' as macros -%}

{%- set active_page = "Upload" -%}
{%- set is_reanalysis = True if firmware else False -%}
{%- set device_part_options = ["complete", "kernel", "bootloader", "root-fs"] -%}

{% block head %}
    <script>
        const allDeviceNames = {{ device_names | safe }};
        let deviceNames = [];
        const plugin_dict = {{ analysis_plugin_dict | dict_to_json | safe }};
    </script>
    <script type="text/javascript" src="{{ url_for('static', filename='js/upload.js') }}"></script>
    <script type="text/javascript" src="{{ url_for('static', filename='js/loading.js') }}"></script>
    <script>
        $(window).bind("pageshow", function (_) {
            // if we come back to the page, make sure that the loading gif is not covering up the form
            hideImg();
        });
        $(document).ready(() => {
            autocompleteInput('device_class', {{ device_classes | tojson | safe }});
            autocompleteInput('vendor', {{ vendors | tojson | safe }});
            autocompleteInput('device_part', {{ device_part_options | tojson | safe }});
            autocompleteInput('device_name', deviceNames);
        });
    </script>
{% endblock %}

{%- block styles -%}
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/upload.css') }}">
    <style>
        .click-through {
            pointer-events: none;
        }
    </style>
{%- endblock -%}

{%- block body -%}

    <div class="row justify-content-center">
        <div class="col-xl-6 col-lg-8 mt-4">
            {%- if is_reanalysis %}
                <h3 class="mb-1">{{ title }} of</h3>
                <h5 class="mb-3">{{ firmware.uid | replace_uid_with_hid }}</h5>
            {%- else %}
                <h3 class="mb-3">Upload Firmware</h3>
            {%- endif %}

            <div id="form">
                <form class="form-horizontal" action="" method=post enctype=multipart/form-data
                      onsubmit="return showImg()">

                    {# File #}
                    <div class="input-group mb-3">
                        {%- if not is_reanalysis %}
                            <div class="input-group-prepend">
                                <span class="input-group-text upload-label">
                                    File
                                </span>
                            </div>
                            <div class="custom-file">
                                <input type="file" class="custom-file-input" id="file" name=file required>
                                <label class="custom-file-label" for="customFile">
                                    Choose file...
                                </label>
                            </div>
                        {%- else %}
                            <input type="hidden" name="file_name" id="file_name" value="{{ firmware.file_name }}">
                        {%- endif %}
                    </div>
                    <script>
                        $(".custom-file-input").on("change", function () {
                            const fileName = $(this).val().split("\\").pop();
                            $(this).siblings(".custom-file-label").addClass("selected").html(fileName);
                        });
                    </script>

                    {# Device Class #}
                    {%- call macros.upload_input("device_class", "Device Class", True) %}
                        <input type="text" name="device_class" class="form-control" id="device_class"
                                {%- if is_reanalysis %} value="{{ firmware.device_class }}" {%- endif -%}
                               placeholder="Select device class..." autocomplete="off" required>
                    {%- endcall %}

                    {# Vendor #}
                    {%- call macros.upload_input("vendor", "Vendor", True) %}
                        <input type="text" name="vendor" class="form-control" id="vendor"
                                {%- if is_reanalysis %} value="{{ firmware.vendor }}" {%- endif -%}
                               placeholder="Select vendor..." autocomplete="off" required>
                    {%- endcall %}

                    {# Device Name #}
                    {%- call macros.upload_input("device_name", "Device Name", True) %}
                        <input type="text" name="device_name" class="form-control" id="device_name"
                                {%- if is_reanalysis %} value="{{ firmware.device_name }}" {%- endif -%}
                               placeholder="Select device name..." autocomplete="off" required>
                    {%- endcall %}

                    {# Device Part #}
                    {%- call macros.upload_input("device_part", "Device Part", True) %}
                        <input type="text" name="device_part" class="form-control" id="device_part"
                                {%- if is_reanalysis and firmware.part %} value="{{ firmware.part }}" {%- endif -%}
                               value="complete" required>
                    {%- endcall %}

                    {# Version #}
                    {%- call macros.upload_input("version", "Version") %}
                        <input type="text" name="version" class="form-control" id="version"
                                {%- if is_reanalysis %} value="{{ firmware.version }}" {%- endif -%}
                               placeholder="Select version..." required>
                    {%- endcall %}

                    {# Release Date #}
                    {%- call macros.upload_input("release_date", "Release Date") %}
                        <input type="text" name="release_date" class="form-control" id="release_date"
                                {%- if is_reanalysis %} value="{{ firmware.release_date }}"{% else %}
                               placeholder="Release Date" {% endif %}>
                    {%- endcall %}

                    {# Tags #}
                    {%- call macros.upload_input("tags", "Tags") %}
                        <input type="text" name="tags" class="form-control" id="tags"
                                {%- if is_reanalysis %} value="{{ ','.join(firmware.tags.keys()) }}"{% else %}
                               placeholder="Optional: Comma separated list (e.g: flashdump,partial)"{% endif %}>
                    {%- endcall %}

                    {# Analysis Preset #}
                    {%- call macros.upload_input("analysis", "Analysis Preset") %}
                        <select class="custom-select" id="analysis" name="analysis_dropdown"
                                onchange="change_selected_plugins(this.value);">
                            {% for preset in analysis_presets %}
                                <option value="{{ preset }}">{{ preset }}</option>
                            {% endfor %}
                        </select>
                    {%- endcall %}

                    <div class="form-group">
                        <div class="col-lg-12 mt-4 px-0"
                             style="column-count: 2;column-rule: 1px hidden black; column-gap:50px">
                            {% for system, (title, mandatory, presets) in analysis_plugin_dict.items() | sort %}
                                {% if not mandatory %}
                                    <div id="{{ system }}">
                                        <label class="checkbox-inline" data-toggle="tooltip"
                                               title="{{ title | safe }}">
                                            <input type=checkbox name="analysis_systems"
                                                   value="{{ system }}"
                                                   {% if presets[selected_preset] %}checked{% endif %}>
                                            <span style="font-family: courier,monospace;">{{ system | replace_underscore }}</span>
                                            <br>
                                        </label>
                                    </div>
                                {%- endif %}
                            {%- endfor %}
                        </div>
                    </div>

                    <button type="submit" value=submit class="btn btn-primary" id="input_submit">
                        {%- if is_reanalysis %}
                            <i class="fas fa-redo-alt"></i> {{ title }}
                        {%- else %}
                            <i class="fas fa-upload"></i> Submit
                        {%- endif %}
                    </button>

                    {%- if is_reanalysis and "re-do" not in title %}
                        <div class="checkbox m-0 pl-2" style="display: inline;">
                            <label style="padding: 2px">
                                <input
                                        type="checkbox" value="true" id="force_reanalysis" style="margin-top: 2px;"
                                        data-toggle="tooltip" title="deactivate smart scheduling"
                                        name="force_reanalysis"
                                > Force Analysis Update
                            </label>
                        </div>
                    {%- endif %}
                </form>
            </div>

            <img id="loading_img" src="{{ url_for('.static', filename='Pacman.gif') }}"
                 alt="loading..." style="display: none; margin: auto;"/>
        </div>
    </div>

{%- endblock -%}
